package com.yjhealth.libs.core.utils.image;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.anlia.photofactory.factory.PhotoFactory;
import com.anlia.photofactory.permission.PermissionAlwaysDenied;
import com.anlia.photofactory.result.ResultData;
import com.yjhealth.libs.core.R;
import com.yjhealth.libs.core.toast.ToastUtil;
import com.yjhealth.libs.core.utils.EffectUtil;
import com.yjhealth.libs.core.utils.FileUtil;
import com.yjhealth.libs.core.view.dialog.CoreConfirmDialog;
import com.yjhealth.libs.core.view.dialog.CoreDialog;

import java.util.Calendar;
import java.util.List;

/**
 * 图片拍照、相册获取;每种获取需要创建一个新的uitl
 */
public class PhotoUtil {
    protected Activity activity;
    protected PhotoFactory photoFactory;

    private OnPhotoResultListener listener;

    public interface OnPhotoResultListener {
        void result(ResultData resultData);
    }

    public PhotoUtil(@NonNull final Activity activity, OnPhotoResultListener listener) {
        this.activity = activity;
        this.listener = listener;

        PhotoFactory.setPermissionAlwaysDeniedAction(new PermissionAlwaysDenied.Action() {
            @Override
            public void onAction(Context context, List<String> permissions, final PermissionAlwaysDenied.Executor executor) {
                List<String> permissionNames = PhotoFactory.transformPermissionText(context, permissions);
                String permissionText = TextUtils.join(context.getString(R.string.yjhealth_core_permission) + "\n", permissionNames);

                CoreConfirmDialog.newInstance(context.getString(R.string.yjhealth_core_photo_permision_title),
                        context.getString(R.string.yjhealth_core_photo_permision_msg, permissionText),
                        context.getString(R.string.yjhealth_core_ok),
                        context.getString(R.string.yjhealth_core_cancel))
                        .setCommonDialogListener(new CoreConfirmDialog.CommonDialogListener() {
                            @Override
                            public void onComplete(boolean ok, String tag) {
                                if (ok) {
                                    executor.toSetting();
                                }
                            }
                        }).show(activity);
            }
        });
    }

    public void showPhotoDialog(final boolean needCrop) {
        final CoreDialog dialog = new CoreDialog(LayoutInflater.from(activity).inflate(R.layout.yjhealth_core_dialog_photo, null));
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);

        LinearLayout llCamera = dialog.findViewById(R.id.llCamera);
        LinearLayout llGallery = dialog.findViewById(R.id.llGallery);

        EffectUtil.addClickEffect(llCamera);
        llCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPhotoFromCamera(needCrop);
                dialog.dismiss();
            }
        });
        EffectUtil.addClickEffect(llGallery);
        llGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPhotoFromeGallery(needCrop);
                dialog.dismiss();
            }
        });

        dialog.show(activity);
    }


    public void getPhotoFromCamera(final boolean needCrop) {
        photoFactory = new PhotoFactory(activity, FileUtil.getStoreImageDir(activity),
                Calendar.getInstance().getTimeInMillis() + ".png");
        photoFactory.FromCamera()
                .AddOutPutExtra()
                .StartForResult(new PhotoFactory.OnResultListener() {
                    @Override
                    public void onCancel() {
                        ToastUtil.toast(R.string.yjhealth_core_photo_photo_cancel);
                    }

                    @Override
                    public void onSuccess(ResultData resultData) {
                        if (resultData == null) {
                            ToastUtil.toast(R.string.yjhealth_core_photo_photo_error);
                            return;
                        }
                        if (needCrop) {
                            getPhotoFromeCrop(resultData.GetUri());
                        } else {
                            if (listener != null) {
                                listener.result(resultData);
                            }
                        }
                    }

                    @Override
                    public void onError(String error) {
                        ToastUtil.toast(R.string.yjhealth_core_photo_photo_error);
                    }
                });
    }


    public void getPhotoFromeGallery(final boolean needCrop) {
        photoFactory = new PhotoFactory(activity, FileUtil.getStoreImageDir(activity),
                Calendar.getInstance().getTimeInMillis() + ".png");
        photoFactory.FromGallery()
                .StartForResult(new PhotoFactory.OnResultListener() {
                    @Override
                    public void onCancel() {
                        ToastUtil.toast(R.string.yjhealth_core_photo_photo_cancel);
                    }

                    @Override
                    public void onSuccess(ResultData resultData) {
                        if (resultData == null) {
                            ToastUtil.toast(R.string.yjhealth_core_photo_photo_error);
                            return;
                        }
                        if (needCrop) {
                            getPhotoFromeCrop(resultData.GetUri());
                        } else {
                            if (listener != null) {
                                listener.result(resultData);
                            }
                        }
                    }

                    @Override
                    public void onError(String error) {
                        ToastUtil.toast(R.string.yjhealth_core_photo_photo_error);
                    }
                });
    }


    public void getPhotoFromeCrop(Uri uri) {
        if (uri == null) {
            return;
        }
        photoFactory.FromCrop(uri)
                .AddAspectX(1)
                .AddAspectY(1)
                .StartForResult(new PhotoFactory.OnResultListener() {
                    @Override
                    public void onCancel() {
                        ToastUtil.toast(R.string.yjhealth_core_photo_photo_cancel);
                    }

                    @Override
                    public void onSuccess(ResultData data) {
                        if (data == null) {
                            ToastUtil.toast(R.string.yjhealth_core_photo_photo_error);
                            return;
                        }
                        if (listener != null) {
                            listener.result(data);
                        }
                    }

                    @Override
                    public void onError(String error) {
                        ToastUtil.toast(R.string.yjhealth_core_photo_photo_error);
                    }
                });
    }
}
